'use strict';

angular.module('xuexibangApp')
  .controller('LoginCtrl', function($scope, Auth, $location, $window) {
    //$scope.user = {code:'banzhuren',name:'班主任'};
    $scope.user={};
    $scope.errors = {};
    $scope.user.password = "";
    $scope.login = function(form) {
      $scope.submitted = true;

      if (form.$valid) {
        Auth.login({
            code: $scope.user.code,
            name: $scope.user.name,
            password: $scope.user.password
          })
          .then(function() {
            // Logged in, redirect to home
            $location.path('/');
          })
          .catch(function(err) {
            if (err.message === "needpass") {
              $scope.errors.other="";
              $(".codename").addClass('hide');
              $(".pass").removeClass('hide');
              $("#password").focus();
            } else {
              $scope.errors.other = err.message;
            }
          });
      }
    };
    $scope.loginOauth = function(provider) {
      $window.location.href = '/auth/' + provider;
    };
  });
